﻿using System;
using OpenGameEngine.Input;
using OpenGameEngine.Base;
using System.Windows.Forms;

namespace OpenGameEngine.GameObjects
{
    public class GameRectangle : GameObject
    {
        private bool _mouseEntered = false;
        private bool _mouseDown = false;
        private MouseStateInfo _prevMouse;
        private MouseStateInfo _mouse;
        private Vector2 _v;
        private RectangleF _d;
        private bool blnCurrMs;
        private bool blnPrevMs;

        private SizeF _size;
        public SizeF Size
        {
            get { return _size; }
            set
            {
                if (_size != value)
                {
                    _size = value;
                }
            }
        }

        public float Width
        {
            get { return _size.Width; }
            set { _size.Width = value; }
        }

        public float Height
        {
            get { return _size.Height; }
            set { _size.Height = value; }
        }

        public GameRectangle()
            : this(0, 0, 0, 0)
        {

        }

        public GameRectangle(float x, float y, float width, float height)
            : base()
        {
            this.Position = new Vector2(x, y);
            this.Size=new SizeF(width,height);
        }

        public override void Update(Base.GameCoreEventArgs e)
        {
            base.Update(e);

            CheckForInput(e);
        }

        private void CheckForInput(GameCoreEventArgs e)
        {
            if (AcceptsMouseInput)
            {
                _prevMouse = e.Core.PreviousMouseStateInfo;
                _mouse = e.Core.CurrentMouseStateInfo;
                _v = _mouse.MouseLocation;

                // check if mouse is inside

                if (_v.X > this.X && _v.Y > this.Y && _v.X < (this.X + this.Width) && _v.Y < (this.Y + this.Height))
                {
                    if (!_mouseEntered)
                    {
                        MouseEnter(e);
                        _mouseEntered = true;
                    }

                    foreach (MouseButtons b in Globals.MouseButtons)
                    {
                        blnCurrMs = _mouse[b];
                        blnPrevMs = _prevMouse[b];

                        if (blnCurrMs)
                        {
                            MouseDown(e, b);
                        }
                        else if (blnPrevMs && !blnCurrMs)
                        {
                            MouseUp(e, b);
                            MouseClick(e, b);
                        }
                    }
                }
                else
                {
                    if (_mouseEntered)
                    {
                        MouseLeave(e);
                        _mouseEntered = false;
                    }
                }
            }
        }

        public static implicit operator RectangleF(GameRectangle rec)
        {
            return new RectangleF(rec.X, rec.Y, rec.Width, rec.Height);
        }

        public static implicit operator GameRectangle(RectangleF rec)
        {
            return new RectangleF(rec.X, rec.Y, rec.Width, rec.Height);
        }
    }
}
